using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Security.Permissions;

namespace WebMenu
{
    /* This program reads a text file 
     * writes out a binary file 
     * checks that the binary file is written correctly.
     */ 
    class Config
    {
        private string sDBname;
        private string sDBuser;
        private string sDBpass;
        private string sDBIPaddress;
        //private const string BINconfigFile = @".\WebMenuConfig";    // in binary format - kept in server
        private const string BINconfigFile_local = @"H:\myCsharp\BeginningASPnet\WebMenu\WebMenuConfig_Localhost";    // in binary format - kept in server
        private const string BINconfigFile_Xhost = @"D:\Domains\checkmenu.org\wwwroot\WebMenuConfig_Xhost";    // in binary format - kept in server


        public void readConfig(ref string sDBnameTmp, ref string sDBuserTmp, ref string sDBpassTmp, ref string sDBIPaddressTmp, ref bool bFile)
        {
            //readHTML();
            ////H:\myCsharp\BeginningASPnet\WebMenu
            //FileInfo fi = new FileInfo(@"WebMenuConfig");
            //if (fi.Exists) { bFile = true; }
            //else { bFile = false; }
            //return;

            try
            {
                //string ss1 = HttpContext.Current.Request.PhysicalApplicationPath;
                //string ss2 = 

                //string webConfigFile = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "web.config");
                //XmlTextReader webConfigReader = new XmlTextReader(new StreamReader(webConfigFile));
                //result = ((webConfigReader.ReadToFollowing("xhtmlConformance")) && (webConfigReader.GetAttribute("mode") == "Legacy"));
                //webConfigReader.Close();
            }
            catch { }

            // Reading the Binary File
            BinaryReader br;
            FileIOPermission f1 = new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read, BINconfigFile_local);
            FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read, BINconfigFile_Xhost);
            if (File.Exists(BINconfigFile_local))     // Local 
            {
                br = new BinaryReader(new FileStream(BINconfigFile_local, FileMode.Open, FileAccess.Read));
            }
            else {                              // Xhost Server
                br = new BinaryReader(new FileStream(BINconfigFile_Xhost, FileMode.Open, FileAccess.Read));
            }

            sDBname = br.ReadString();
            sDBuser = br.ReadString();
            sDBpass = br.ReadString();
            sDBIPaddress = br.ReadString();
            br.Close();

            //Decoding 
            sDBnameTmp = this.DecodeFrom64(sDBname);
            sDBuserTmp = this.DecodeFrom64(sDBuser);
            sDBpassTmp = this.DecodeFrom64(sDBpass);
            sDBIPaddressTmp = this.DecodeFrom64(sDBIPaddress);
        }


        private string DecodeFrom64(string encodedData)
        {
            byte[] encodedDataAsBytes
                = System.Convert.FromBase64String(encodedData);
            string returnValue =
               System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
            return returnValue;
        }
        
        
        //private void readHTML()
        //{
        //    string url = @"http://www.checkmenu.org/WebMenuConfig.html";
        //    string result; 
        //    WebResponse objResponse; 
        //    WebRequest objRequest = System.Net.HttpWebRequest.Create(url); 
        //    objResponse = objRequest.GetResponse(); 

        //    using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()) ) 
        //    {
        //        result = sr.ReadLine();
        //        result = sr.ReadLine();
        //        result = sr.ReadLine();
        //        result = sr.ReadLine();
        //        // Close and clean up the StreamReader 
        //        sr.Close(); 
        //    } 
        //} // end readHTML

    
    }
}
